package rec

import (
	//"database/sql"
	//"encoding/json"
	//"errors"
	//"fmt"
	//"db2s/Er"
	//greatdb "db2s/Gr"
	//mysql "db2s/My"
	"db2s/global"
	//"db2s/outPut"
	//"reflect"
	//"strings"
	//"time"
)

type RecoverObject struct {
	Variables bool
	Mode      string
	TimeStamp string
}

func unmarshal(s string) (res global.ModifyParameterResultS, err error) {
	//if err = json.Unmarshal([]byte(s), &res); err != nil {
	//	return
	//}
	return
}
func filter(q RecoverObject, m global.ModifyParameterResultS) bool {
	//switch {
	//case q.Variables:
	//	if strings.EqualFold(m.Object, "variables") && strings.EqualFold(q.Mode, m.Mode) && strings.EqualFold(q.TimeStamp, m.Time) {
	//		return true
	//	}
	//}
	return false
}
func readParameterRecordBody(file string, q RecoverObject) (res []global.ModifyParameterResultS) {
	//var rr global.ModifyParameterResultS
	//var err error
	//fmt.Println(fmt.Sprintf("-- [%v] %v report: begin read recover file %v -- ", time.Now().Format("2006-01-02 15:04:05"), global.ToolName, file))
	//ff := outPut.FileOut("text", map[string]any{"fileName": file, "rowsS": "\n"})
	//m := ff.Read1()
	//for {
	//	select {
	//	case c, ok := <-m.(chan any):
	//		if !ok {
	//			return
	//		}
	//		switch fmt.Sprintf("%v", reflect.TypeOf(c)) {
	//		case "*string":
	//			if rr, err = unmarshal(*c.(*string)); err != nil {
	//				fmt.Println(fmt.Sprintf("-- [%v] %v report: [warn] string %v unmarshal fail. err={%v} -- ", time.Now().Format("2006-01-02 15:04:05"), global.ToolName, *c.(*string), err))
	//				continue
	//			}
	//			if !filter(q, rr) {
	//				continue
	//			}
	//			res = append(res, rr)
	//		}
	//	}
	//}
	return
}
//func MyParameterSet(s global.DbParameterOptimizerS, rr global.ModifyParameterResultS) (err error) {
//	setSql := fmt.Sprintf("set global %v = %v", rr.ParameterName, rr.OldValue)
//	if _, err = mysql.ExecUpdateSql(mysql.ExecSqlInputS{
//		Ss:           s,
//		UpdateSqlStr: []string{setSql},
//	}); err != nil {
//		return
//	}
//	return
//}
//func greatParameterSet(s global.DbParameterOptimizerS, rr global.ModifyParameterResultS) (err error) {
//	setSql := fmt.Sprintf("set global %v = %v", rr.ParameterName, rr.OldValue)
//	if _, err = greatdb.ExecUpdateSql(greatdb.ExecSqlInputS{
//		Ss:           s,
//		UpdateSqlStr: []string{setSql},
//	}); err != nil {
//		return
//	}
//	return
//}
//func oracleParameterSet(s global.DbParameterOptimizerS, rr global.ModifyParameterResultS) (err error) {
//	return
//}
//func sqlserverParameterSet(s global.DbParameterOptimizerS, rr global.ModifyParameterResultS) (err error) {
//	return
//}

// parameterSet 参数设置
//func parameterSet(s global.DbParameterOptimizerS, rr global.ModifyParameterResultS) (err error) {
//	switch rr.Con.Device {
//	case "mysql":
//		return MyParameterSet(s, rr)
//	case "cluster":
//		return greatParameterSet(s, rr)
//	case "oracle":
//		return oracleParameterSet(s, rr)
//	case "sqlserver":
//		return sqlserverParameterSet(s, rr)
//	default:
//		err = errors.New(fmt.Sprintf("bad drive"))
//	}
//	return
//}

//func setVariables(db *sql.DB, rr global.ModifyParameterResultS) (err error) {
//	var s = global.DbParameterOptimizerS{
//		DB:        map[string]*sql.DB{"single": db},
//		ShardName: "single",
//	}
//	return parameterSet(s, rr)
//}
//func openDb(rr global.Conn) (db *sql.DB, err error) {
//	var t Er.TableConnEr
//	var f1 global.ConnPool
//	if t, err = Er.CActive(Er.ConnActive{DBType: rr.Device}); err != nil {
//		return
//	}
//	rr.Optimizer = false
//	if f1, err = t.Open(rr); err != nil {
//		return
//	}
//	db = f1.Single
//	return
//}
func recoverAction(rr []global.ModifyParameterResultS) (err error) {
	//var db *sql.DB
	//for _, r := range rr {
	//	switch r.Object {
	//	case "variables":
	//		if db, err = openDb(r.Con); err != nil {
	//			fmt.Println(fmt.Sprintf("-- [%v] %v report: [warn] open %v db connection fail. err={%v} -- ", time.Now().Format("2006-01-02 15:04:05"), global.ToolName, r.Con.Device, err))
	//			continue
	//		}
	//		if setVariables(db, r) != nil {
	//			fmt.Println(fmt.Sprintf("-- [%v] %v report: [warn] update variables %v fail. err={%v} -- ", time.Now().Format("2006-01-02 15:04:05"), global.ToolName, r.ParameterName, err))
	//			continue
	//		}
	//	}
	//}
	return
}
func (r RecoverObject) ParameterValue(file string) {
	//fmt.Println(fmt.Sprintf("-- [%v] %v report: begin exec recover action -- ", time.Now().Format("2006-01-02 15:04:05"), global.ToolName))
	//rr := readParameterRecordBody(file, r)
	//if recoverAction(rr) != nil {
	//	fmt.Println(fmt.Sprintf("-- [%v] %v report: exec recover action fail !!! -- ", time.Now().Format("2006-01-02 15:04:05"), global.ToolName))
	//	return
	//}
	//fmt.Println(fmt.Sprintf("-- [%v] %v report: exec recover action finish !!! -- ", time.Now().Format("2006-01-02 15:04:05"), global.ToolName))
}
